# -*- coding:utf-8 -*-
"""
16进制 0-9 a-f
150-300/天
&#xf65d   &#xecdc  &#xefff  -  &#xe065  &#xefff  &#xefff/天
   1        5         0     -      3        0       0
xefff --> 0 --> uni30 --> 61439

字体文件（.woff  .ttf）  ---  有时效性
# 由于字体文件有实效性，下面这个替换的方式不太实用
new_resp = response.text.replace("&#xf65d", "1").replace("&#xecdc", "5").replace("&#xefff", "0").replace("&#xe065", "3")

"""


from fontTools.ttLib import TTFont
import requests
import re
import os

if not os.path.exists("实习僧反爬"):
    os.mkdir("实习僧反爬")

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}

url = "https://www.shixiseng.com/interns?keyword=python&city=%E5%85%A8%E5%9B%BD&type=intern"

response = requests.get(url, headers)
# print(response.text)

# 每次代码运行下载最新的字体文件
font_url = "https://www.shixiseng.com" + re.search(r'url\((.*?)\);}', response.text).group(1)
resp = requests.get(font_url).content
with open("./实习僧反爬/" + "file.woff", "wb") as f:
    f.write(resp)

# 将字体文件转化为xml文件
font = TTFont("./实习僧反爬/file.woff")
font.saveXML("实习僧.xml")
glyf = font["glyf"]
glyf_dict = {
    0: glyf["uni30"],
    1: glyf["uni31"],
    2: glyf["uni32"],
    3: glyf["uni33"],
    4: glyf["uni34"],
    5: glyf["uni35"],
    6: glyf["uni36"],
    7: glyf["uni37"],
    8: glyf["uni38"],
    9: glyf["uni39"],
}


cmap_dict = font.getBestCmap()
response = response.text.replace("&#", str(0))

# 遍历 字体对应关系的字典，键是16进制  值是unixxx
for key_data, value_data in cmap_dict.items():
    key_data = hex(key_data)
    value_data = glyf[value_data]
    for key_new, value_new in glyf_dict.items():
        if value_new == value_data:
            response = re.sub(key_data, str(key_new), response)

print(response)

